/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-04-02
 * Time: 19:27
 */
public class test_1 {
    public static volatile int COUNTER = 0;
    private static Object lock = new Object();
    public static void main(String[] args) {
        Thread t2 = new Thread(() -> {
                synchronized (lock) {
                    while (COUNTER % 3 != 0) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    COUNTER++;
                    lock.notifyAll();
                }
        }, "C");
        Thread t1 = new Thread(() -> {
                synchronized (lock) {
                    while (COUNTER % 3 != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    COUNTER++;
                    lock.notifyAll();
                }
        }, "B");
        Thread t3 = new Thread(() -> {
                synchronized (lock) {
                    while (COUNTER % 3 != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    COUNTER++;
                    lock.notifyAll();
            }
        }, "A");
        t1.start();
        t2.start();
        t3.start();

    }
}
